package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dto.WarehouseDTO;
import com.itheima.mapper.WarehouseMapper;
import com.itheima.pojo.Warehouse;
import com.itheima.service.WarehouseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service

public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {
    @Override
    public Page<WarehouseDTO> getWarehousePage(Page<WarehouseDTO> page, WarehouseDTO dto) {
        // 使用PageHelper进行分页
        PageHelper.startPage((int)page.getCurrent(), (int)page.getSize());

        // 构造查询条件
        LambdaQueryWrapper<Warehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Warehouse::getIsDel, 1); // 只查询未删除的

        if (dto != null) {
            if (dto.getName() != null && !dto.getName().isEmpty()) {
                wrapper.like(Warehouse::getName, dto.getName());
            }
            if (dto.getCityName() != null && !dto.getCityName().isEmpty()) {
                wrapper.like(Warehouse::getCityName, dto.getCityName());
            }
            if (dto.getPersonChargeName() != null && !dto.getPersonChargeName().isEmpty()) {
                wrapper.like(Warehouse::getPersonChargeName, dto.getPersonChargeName());
            }
        }

        // 执行查询
        List<Warehouse> warehouseList = this.list(wrapper);
        PageInfo<Warehouse> pageInfo = new PageInfo<>(warehouseList);

        // 转换为DTO页面
        Page<WarehouseDTO> dtoPage = new Page<>(page.getCurrent(), page.getSize(), pageInfo.getTotal());
        dtoPage.setRecords(convertToDtoList(warehouseList));
        return dtoPage;
    }

    @Override
    public Warehouse getWarehouseById(Integer id) {
        LambdaQueryWrapper<Warehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Warehouse::getId, id)
                .eq(Warehouse::getIsDel, 1);
        return this.getOne(wrapper);
    }

    @Override
    public boolean saveWarehouse(Warehouse warehouse) {
        if (warehouse.getId() == null) {
            warehouse.setCreateTime(LocalDateTime.now());
            return this.save(warehouse);
        } else {
            warehouse.setUpdateTime(LocalDateTime.now());
            return this.updateById(warehouse);
        }
    }

    @Override
    public boolean updateWarehouse(Warehouse warehouse) {
        warehouse.setUpdateTime(LocalDateTime.now());
        return this.updateById(warehouse);
    }

    @Override
    public boolean deleteWarehouse(Integer id) {
        Warehouse warehouse = new Warehouse();
        warehouse.setId(id);
        warehouse.setIsDel(2); // 标记为已删除
        return this.updateById(warehouse);
    }

    @Override
    public List<Warehouse> listAll() {
        LambdaQueryWrapper<Warehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Warehouse::getIsDel, 1);
        return this.list(wrapper);
    }
    private WarehouseDTO convertToDto(Warehouse warehouse) {
        WarehouseDTO dto = new WarehouseDTO();
        BeanUtils.copyProperties(warehouse, dto);
        // 设置是否有效显示字段
        dto.setIsValid(warehouse.getIsDel());
        return dto;
    }

    private List<WarehouseDTO> convertToDtoList(List<Warehouse> warehouses) {
        return warehouses.stream().map(this::convertToDto).toList();
    }
}
